extends "res://scenes/Scene.gd"


onready var point0 := $PointBox0
onready var point1 := $PointBox1
onready var point2 := $PointBox2
onready var point3 := $PointBox3
onready var point4 := $PointBox4
onready var point5 := $PointBox5
onready var point6 := $PointBox6

#var _init_idx = [3,6,2,1,4,5]
var _init_idx = [1,2,3,4,5,0]
var pointSwitchOnTextures = ["res://assets/H2A/SS_01.png","res://assets/H2A/SS_02.png", 
"res://assets/H2A/SS_03.png", "res://assets/H2A/SS_04.png"
, "res://assets/H2A/SS_05.png", "res://assets/H2A/SS_06.png"]
var pointSwitchOffTextures = ["res://assets/H2A/SS_07.png","res://assets/H2A/SS_08.png", 
"res://assets/H2A/SS_09.png", "res://assets/H2A/SS_10.png"
, "res://assets/H2A/SS_11.png", "res://assets/H2A/SS_12.png"]


var pointBoxes = []
var pointLines = [[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[1,2],[1,3],[1,6],[3,4],[4,6],[5,6]]
func _ready():
	pointBoxes.append(point0)
	pointBoxes.append(point1)
	pointBoxes.append(point2)
	pointBoxes.append(point3)
	pointBoxes.append(point4)
	pointBoxes.append(point5)
	pointBoxes.append(point6)
	
	connect("point_changed", self, "_on_point_position_change")
	
	for li in pointLines:
		# 点两端互相添加对方
		pointBoxes[li[0]].add_connect_boxes(pointBoxes[li[1]])
		pointBoxes[li[1]].add_connect_boxes(pointBoxes[li[0]])
		_add_line(pointBoxes[li[0]].position, pointBoxes[li[1]].position)
	
	_reset()
	
	
func _add_line(vec_src, vec_dst):
	var lens = vec_src.distance_to(vec_dst)
	var degree = vec_src.angle_to_point(vec_dst)
	var line_scene = load("res://scenes/turn/CircleLine.tscn")
	var line_obj = line_scene.instance()
	line_obj.scale.x = lens / 53
	line_obj.rotation = degree
	line_obj.position = Vector2((vec_src.x + vec_dst.x) / 2, (vec_src.y + vec_dst.y) / 2)
	$Lines.add_child(line_obj)
	   
func _reset():
	point0.clear()
	var point_scene = load("res://scenes/turn/Point.tscn")
	var pidx = 1
	for idx in _init_idx:
		for it in pointBoxes:
			if it.box_index != idx:
				continue
			it.clear()
			
			var point = point_scene.instance()
			point.point_index = pidx
			point.switch_on_texture = pointSwitchOnTextures[pidx - 1]
			point.switch_off_texture = pointSwitchOffTextures[pidx - 1]
			
			it.set_point(point)
			
			pidx += 1


func _on_click_reset():
	var tween = create_tween()
	tween.set_ease(Tween.EASE_OUT).set_trans(Tween.TRANS_QUART)
	tween.tween_property($Interactable/Sprite2, "rotation", $Interactable/Sprite2.rotation + 15.0, 0.6)
	tween.tween_callback(self, "_reset")
	
func _on_point_position_change():
	for it in range(1, pointBoxes.size()):
		if not pointBoxes[it].is_valid_point():
			return

	Game.flags.add_flag("h2a_passed")
	SceneChanger.change_scene("res://scenes/H3.tscn")

